package com.flydog.mall.service.impl.pms;

import cn.hutool.core.util.StrUtil;
import com.flydog.mall.common.api.PageResponse;
import com.flydog.mall.dto.PmsBrandParam;
import com.flydog.mall.entity.pms.PmsBrand;
import com.flydog.mall.repository.pms.PmsBrandRepository;
import com.flydog.mall.service.FileManageService;
import com.flydog.mall.service.pms.PmsBrandService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author Cao
 * @description 品牌管理实现类
 * @create 2021-03-18
 */
@Service
@Slf4j
public class PmsBrandServiceImpl implements PmsBrandService {

    @Autowired
    private PmsBrandRepository brandRepository;

    @Autowired
    private FileManageService fileManageService;

    @Override
    public int create(PmsBrandParam brandParam) {
        PmsBrand brand = new PmsBrand();
        BeanUtils.copyProperties(brandParam, brand);
        brandRepository.save(brand);
        log.info("新品牌创建{}", brand);
        return 1;
    }

    @Override
    public Optional<PmsBrand> getBrand(long id) {
        return brandRepository.findById(id);
    }

    @Override
    public PageResponse<PmsBrand> listBrand(String keyword, int pageNum, int pageSize) {
        PageRequest request = PageRequest.of(pageNum - 1, pageSize, Sort.Direction.ASC, "sort");
        Page<PmsBrand> pageResult;
        if (StrUtil.isBlankIfStr(keyword)) {
            pageResult = brandRepository.findAll(request);
        } else {
            pageResult = brandRepository.findByNameContaining(keyword, request);
        }
        return PageResponse.restPage(pageResult);
    }

    @Override
    public List<PmsBrand> listAll() {
        return brandRepository.findAll(Sort.by("sort"));
    }

    @Override
    public int delete(long id) throws Exception {
        Optional<PmsBrand> sourceBrand = brandRepository.findById(id);
        if (sourceBrand.isPresent()) {
            String bigPigUrl = sourceBrand.get().getBigPic();
            String logoUrl = sourceBrand.get().getLogo();
            fileManageService.deleteByUrl(bigPigUrl);
            fileManageService.deleteByUrl(logoUrl);
            brandRepository.deleteById(id);
        }

        return 1;
    }

    @Override
    public int update(long id, PmsBrandParam brand) {
        PmsBrand brandDao = new PmsBrand();
        BeanUtils.copyProperties(brand, brandDao);
        brandDao.setId(id);
        brandRepository.save(brandDao);
        return 1;
    }

    @Override
    public int updateFactoryStatus(List<Long> ids, Integer factoryStatus) {
        for (Long id : ids) {
            Optional<PmsBrand> temp = brandRepository.findById(id);
            if (temp.isPresent()) {
                temp.get().setFactoryStatus(factoryStatus);
                brandRepository.save(temp.get());
            }
        }
        return ids.size();
    }

    @Override
    public int updateShowStatus(List<Long> ids, Integer showStatus) {
        for (Long id : ids) {
            Optional<PmsBrand> temp = brandRepository.findById(id);
            if (temp.isPresent()) {
                temp.get().setShowStatus(showStatus);
                brandRepository.save(temp.get());
            }
        }
        return ids.size();
    }
}
